package com.groupfour.attendancemanagementsystem.controller;

import com.groupfour.attendancemanagementsystem.entity.DTO.CourseAndGroupDTO;
import com.groupfour.attendancemanagementsystem.entity.VO.*;
import com.groupfour.attendancemanagementsystem.service.GroupRatingService;
import com.groupfour.attendancemanagementsystem.utils.JwtHelper;
import com.groupfour.attendancemanagementsystem.utils.Result;
import com.groupfour.attendancemanagementsystem.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @version: V1.0
 * @author: 章珑赢
 * @className: GroupRatingController
 * @packageName: controller
 * @description: 小组评分
 * @data: 2022/4/23
 */
@Controller
@RequestMapping("/groupRating")
public class GroupRatingController {
    @Autowired
    private GroupRatingService groupRatingService;
    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: SelectAllGroupRating
     * @description: 查询所有小组评分
     * @param:  pageNum
     * @return: Result
     * @throws: 无
     **/
    @RequestMapping("/SelectAllGroupRating/{pageNum}/{pageSize}")
    @ResponseBody
    public Result SelectAllGroupRating(
            @PathVariable("pageNum") Integer pageNum ,
            @PathVariable("pageSize") Integer pageSize,
            @RequestHeader("token") String token) {
        boolean expiration = JwtHelper.isExpiration(token);
        if (expiration) {
            return Result.build(null, ResultCodeEnum.TOKEN_ERROR);
        }
        //        从token中解析出用户的学生学号
        String userCode = JwtHelper.getUserCode(token);
        Integer userType = JwtHelper.getUserType(token);

        Map<String,Object> groupRatingVOMap= null;
        switch (userType) {
            case 0:
                groupRatingVOMap= groupRatingService.SelectAllGroupRating(pageNum,pageSize,"");
                break;
            case 2:
                groupRatingVOMap= groupRatingService.SelectAllGroupRating(pageNum,pageSize,userCode);
                break;
        }

        if ( (Long) groupRatingVOMap.get("pageCount") > 0) {
            // 通过查询到的项目小组的编号集合进行循环查询小组人员数量
            return Result.ok(groupRatingVOMap);
        } else {
            return Result.fail().message("出错了");
        }
    }
    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: SelectByProjectNameOrGroupName
     * @description: 根据项目名称或小组名称查询所有小组评分
     * @param:  projectName(项目名称)，groupName(小组评分)
     * @return: Result
     * @throws: 无
     **/
    @RequestMapping("/SelectGroupByProjectNameOrGroupName/{pageNum}/{pageSize}")
    @ResponseBody
    public Result SelectGroupByProjectNameOrGroupName(
            @RequestBody CourseAndGroupDTO courseAndGroup,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @RequestHeader("token") String token) {

        boolean expiration = JwtHelper.isExpiration(token);
        if (expiration) {
            return Result.build(null, ResultCodeEnum.TOKEN_ERROR);
        }
        //        从token中解析出用户的学生学号
        String userCode = JwtHelper.getUserCode(token);
        Integer userType = JwtHelper.getUserType(token);

        Map<String,Object> groupRatingVOMap= null;
        switch (userType) {
            case 0:
                groupRatingVOMap = groupRatingService.SelectGroupRating(courseAndGroup.getCourse(), courseAndGroup.getGroup(),pageNum,pageSize,"");
                break;
            case 2:
                groupRatingVOMap = groupRatingService.SelectGroupRating(courseAndGroup.getCourse(), courseAndGroup.getGroup(),pageNum,pageSize,userCode);
                break;
        }
        if ( (Long) groupRatingVOMap.get("pageCount") > 0) {
            return Result.ok(groupRatingVOMap);
        } else {
            return Result.fail().message("出错了");
        }
    }

    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: SelectGroupDetails
     * @description: 查询小组的详情
     * @param:  groupCode（小组编号）
     * @return: Result
     * @throws: 无
     **/
    @RequestMapping("/SelectGroupDetails/{groupCode}")
    @ResponseBody
    public Result SelectGroupDetails(@PathVariable String groupCode) {
        GroupDetailVO groupDetailVOS = groupRatingService.SelectGroupDetails(groupCode);
        if (groupDetailVOS != null) {
            return Result.ok(groupDetailVOS);
        } else {
            return Result.fail().message("出错了");
        }
    }
    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: SelectHomeWorks
     * @description: 查询小组的作业信息
     * @param:  groupCode（小组编号）
     * @return: Result
     * @throws: 无
     **/
    @RequestMapping("/SelectHomeWorks/{pageNum}/{pageSize}")
    @ResponseBody
    public Result SelectGroupHomeWorks(
            String groupCode,
            String studentCode,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) {
        Map<String,Object> homeWorkDetailsVOMap = groupRatingService.SelectHomeWorks(groupCode,studentCode,pageNum,pageSize);
        if ( (Long) homeWorkDetailsVOMap.get("pageCount") > 0) {
            return Result.ok(homeWorkDetailsVOMap);
        } else {
            return Result.fail().message("出错了");
        }
    }
    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: DoGroupGradeAndComment
     * @description: 进行小组打分和评论
     * @param:  groupCode（小组编号），groupSyntnesisScore（小组分数），groupComment（小组评论）
     * @return: Result
     * @throws: 无
     **/
    @RequestMapping("/DoGroupGradeAndComment/{groupCode}")
    @ResponseBody
    public Result DoPersonalGradeAndComment(String groupSyntnesisScore, String groupComment, @PathVariable String groupCode){
        boolean success=groupRatingService.DoGroupGradeAndComment(groupSyntnesisScore,groupComment,groupCode);
        if(success){
            return Result.ok();
        }
        return Result.fail("保存失败");
    }
    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: selectDepartmentAndGroup
     * @description: 进行小组打分和评论
     * @param:  无
     * @return: Result
     * @throws: 无
     **/
    @RequestMapping("/selectCourseAndGroup")
    @ResponseBody
    public Result selectDepartmentAndGroup(){
       DepartmentAndGroupVO departmentAndGroup=groupRatingService.selectCourseAndGroup();
        if(departmentAndGroup!=null){
            return Result.ok(departmentAndGroup);
        }
        return Result.fail("保存失败");
    }
}
